﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace SharedLib.Helpers
{
    public class Math
    {
        public static double Angle(double px1, double py1, double px2, double py2)
        {
            double pxRes = px2 - px1;
            double pyRes = py2 - py1;
            double angle = 0.0;

            if (pxRes == 0.0)
            {
                if (pxRes == 0.0)
                    angle = 0.0;
                else if (pyRes > 0.0) angle = System.Math.PI / 2.0;
                else
                    angle = System.Math.PI * 3.0 / 2.0;
            }
            else if (pyRes == 0.0)
            {
                if (pxRes > 0.0)
                    angle = 0.0;
                else
                    angle = System.Math.PI;
            }
            else
            {
                if (pxRes < 0.0)
                    angle = System.Math.Atan(pyRes / pxRes) + System.Math.PI;
                else if (pyRes < 0.0) angle = System.Math.Atan(pyRes / pxRes) + (2 * System.Math.PI);
                else angle = System.Math.Atan(pyRes / pxRes);

            }
            return angle;
        }

        public static int Distance2D(int x1, int y1, int x2, int y2)
        {
            //     ______________________
            //d = &#8730; (x2-x1)^2 + (y2-y1)^2
            //

            //Our end result
            int result = 0;
            //Take x2-x1, then square it
            double part1 = System.Math.Pow((x2 - x1), 2);
            //Take y2-y1, then sqaure it
            double part2 = System.Math.Pow((y2 - y1), 2);
            //Add both of the parts together
            double underRadical = part1 + part2;
            //Get the square root of the parts
            result = (int)System.Math.Sqrt(underRadical);
            //Return our result
            return result;
        }

        public static Vector2 RotateAboutOrigin(Vector2 point, Vector2 origin, float rotation)
        {
            Vector2 u = point - origin; //point relative to origin  

            if (u == Vector2.Zero)
                return point;

            float a = (float)System.Math.Atan2(u.Y, u.X); //angle relative to origin  
            a += rotation; //rotate  

            //u is now the new point relative to origin  
            u = u.Length() * new Vector2((float)System.Math.Cos(a), (float)System.Math.Sin(a));
            return u + origin;
        } 
    }
}
